﻿using System;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data;
using ART.BLL;
using ART.Common;

namespace ART.Admini.Setting
{
    public partial class DictionaryList : System.Web.UI.Page
    {
        private BLL.B_dictionary bll = new B_dictionary();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(repMainDictionary, bll.GetList("ParentId=0").Tables[0]);
            //JBind.BindList(rptDictionary, new B_mainDictionary().GetList("").Tables[0]);

            #region __________Where语句__________

            string SqlWhere = "  1=1";
            if (MainDictionaryId>0)
            {
                SqlWhere += " and charindex('," + MainDictionaryId + ",',colpath)>0 ";
            }
            
            #endregion

            rptDictionary.PageLink = "DictionaryList.aspx?MainDictionaryId=" + MainDictionaryId;
            rptDictionary.PageSize = 20;
            rptDictionary.CurrentPage = LYRequest.GetInt("page", 1);

            DataSet ds = BLL.GetListByPage.GetDataSet_NoCache("dictionary", "*", SqlWhere, "", "Sort desc,Id asc", "Id", rptDictionary.CurrentPage, rptDictionary.PageSize, 1);
            
            rptDictionary.RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
            if (MainDictionaryId > 0 && rptDictionary.RecordCount>0)
            {
                rptDictionary.RecordCount = rptDictionary.RecordCount - 1;
            }
            DataTable dt1 = JTree.GetTreeTable(ds.Tables[1], "ParentId", MainDictionaryId.ToString(), "DictionaryName");
            JBind.BindList(rptDictionary, dt1);
        }
        protected string GetIfDelete(string ifDelete)
        {
            if (ifDelete=="0")
            {
                return "<span class='red'>(不可以删除)</span>";
            }
            return "";
        }

        protected void lbtnDeletes_Click(object sender, EventArgs p1)
        {
            //int num = 0;
            foreach (RepeaterItem current in rptDictionary.Items)
            {
                HtmlInputCheckBox box = (HtmlInputCheckBox)current.FindControl("cheId");
                if (box.Checked && (this.bll.GetModel(int.Parse(box.Value)).IfDelete)==1)
                {
                    bll.Delete(int.Parse(box.Value));
                   // num++;
                }
            }
            
            //if (num > 0)
            //{
            //    B_operationLog.CreateOperationLog(string.Format("共批量删除{0}条字典记录", num));
            //}
            base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
        }
        protected void rptDictionary_ItemCommand(object source, RepeaterCommandEventArgs p1)
        {
            int num = int.Parse(p1.CommandArgument.ToString());
            if (p1.CommandName == "lbtnDelete")
            {
                if (bll.GetModel(num).IfDelete==1)
                {
                    bll.Delete(num);
                }
                base.Response.Redirect(HttpContext.Current.Request.Url.PathAndQuery);
            }
        }
        protected int MainDictionaryId
        {
            get
            {
                int num = 0;
                if (!string.IsNullOrEmpty(base.Request.QueryString["MainDictionaryId"]))
                {
                    num = int.Parse(base.Request.QueryString["MainDictionaryId"]);
                }
                return num;
            }
        }

        public string MainDictionaryName
        {
            get
            {
                if (MainDictionaryId==0)
                {
                    return "全部";
                }
                return bll.GetModel(this.MainDictionaryId).DictionaryName;
            }
        }
    }
}
